package com.zhu.service.edu.controller.Front;


import com.zhu.commons.utils.JwtInfo;
import com.zhu.commons.utils.JwtUtils;
import com.zhu.commons.utils.R;
import com.zhu.service.edu.pojo.EduCourseCollect;
import com.zhu.service.edu.pojo.vo.CourseCollectVo;
import com.zhu.service.edu.service.IEduCourseCollectService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 课程收藏 前端控制器
 * </p>
 *
 * @author zhu
 * @since 2022-07-04
 */
@RestController
@RequestMapping("api/edu/course/collect")
public class EduCourseCollectController {
    /**
     * 1.收藏课程
     * 2.收藏列表
     * 3.取消收藏
     * 4.判断是否收藏
     */
    @Autowired
    private IEduCourseCollectService courseCollectService;

    @ApiOperation(value = "添加收藏")
    @PostMapping("/auth/addCollect")
    public R  addCollect(HttpServletRequest request,String courseId){
        JwtInfo memberIdByJwtToken = JwtUtils.getMemberIdByJwtToken(request);
        if(memberIdByJwtToken==null){
            return R.error().message("请登录！");
        }
        String id = memberIdByJwtToken.getId();
        EduCourseCollect courseCollect=new EduCourseCollect();
        courseCollect.setCourseId(courseId);
        courseCollect.setMemberId(id);

        if(courseCollectService.save(courseCollect)){
            return R.ok().message("收藏成功");
        }
        return R.error().message("收藏失败！");
    }
    @ApiOperation(value = "删除收藏")
    @DeleteMapping("/auth/removeCollect/{courseId}")
    public R  removeCollect(HttpServletRequest request,@PathVariable String courseId){
        JwtInfo memberIdByJwtToken = JwtUtils.getMemberIdByJwtToken(request);
        if(memberIdByJwtToken==null){
            return R.error().message("请登录！");
        }
        String id = memberIdByJwtToken.getId();


        if(courseCollectService.removeCollect(courseId,id)){
            return R.ok().message("取消收藏");
        }
        return R.error().message("error！");
    }
    @ApiOperation(value = "收藏列表")
    @GetMapping("/auth/collectList")
    public R  collectList(HttpServletRequest request){
        JwtInfo memberIdByJwtToken = JwtUtils.getMemberIdByJwtToken(request);
        if(memberIdByJwtToken==null){
            return R.error().message("请登录！");
        }
        String id = memberIdByJwtToken.getId();
        List<CourseCollectVo> collectVoList = courseCollectService.collectList(id);
        return R.ok().data("items",collectVoList);
    }

    @ApiOperation(value = "判断是否收藏")
    @GetMapping("/auth/isCollect/{courseId}")
    public R  collectList(HttpServletRequest request,@PathVariable String courseId){
        JwtInfo memberIdByJwtToken = JwtUtils.getMemberIdByJwtToken(request);
        if(memberIdByJwtToken==null){
            return R.error().message("请登录！");
        }
        String id = memberIdByJwtToken.getId();
        Boolean isCollect= courseCollectService.isCollect(id,courseId);
        return R.ok().data("items",isCollect);
    }
}
